package com.xiaoshuidi.cloud.module.report.controller.finance;


import cn.hutool.core.util.ObjectUtil;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;
import com.xiaoshuidi.cloud.framework.security.core.LoginUser;
import com.xiaoshuidi.cloud.module.report.controller.finance.vo.HouseOwnerDepositExportRespVO;
import com.xiaoshuidi.cloud.module.report.controller.finance.vo.HouseOwnerDepositReqVO;
import com.xiaoshuidi.cloud.module.report.controller.finance.vo.HouseOwnerDepositRespVO;
import com.xiaoshuidi.cloud.module.report.convert.finance.ReportHouseOwnerDepositConvert;
import com.xiaoshuidi.cloud.module.report.dal.DO.report.ReportHouseOwnerDepositDO;
import com.xiaoshuidi.cloud.module.report.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.report.service.ajreport.ReportHouseOwnerDepositService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;
import static com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

/**
 * 业主押金报表
 */

@Slf4j
@Tag(name = "管理后台 -业主押金报表")
@RestController
@RequestMapping(ApiConstants.PREFIX + "/house-owner/deposit")
public class ReportHouseOwnerDepositController {

    @Resource
    private ReportHouseOwnerDepositService reportHouseOwnerDepositService;


    /**
     * 业主押金报表分页查询
     *
     * @param reqVO
     * @return
     */
    @PostMapping("/page")
    @Operation(summary = "业主押金报表分页查询")
//    @PermitAll
    public CommonResult<HouseOwnerDepositRespVO> page(@RequestBody @Valid HouseOwnerDepositReqVO reqVO) {
        log.info("DepositDetailReportController page req{}", reqVO.toString());
        return success(reportHouseOwnerDepositService.page(reqVO));
    }


    /**
     * 业主押金报表出
     *
     * @return
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, HouseOwnerDepositReqVO reqVO) throws IOException {
        reqVO.setPageSize(100);
        LoginUser loginUser = getLoginUser();
        if (ObjectUtil.isNotNull(loginUser)) {
            reqVO.setTenantId(loginUser.getTenantId());
        }
        PageResult<ReportHouseOwnerDepositDO> pageResult = reportHouseOwnerDepositService.selectPageList(reqVO);
        ExcelUtils.write(response, "业主押金明细.xlsx", "流水明细", HouseOwnerDepositExportRespVO.class, pageResult,
                ReportHouseOwnerDepositConvert.INSTANCE::convertPageList, (pageNo) -> {
                    reqVO.setPageNo(pageNo);
                    return reportHouseOwnerDepositService.selectPageList(reqVO);
                });
    }

}
